﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormAssigment.Entities;
using WinFormAssigment.DAO;
using WinFormAssigment.BUS;

namespace WinFormAssigment
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            tbPassword.PasswordChar = '*';
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();                        
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            UserLogin();
            
        }

        private void UserLogin()
        {
            if ((!Validation.IsEmptyTextBox(new TextBox[] { tbUsername, tbPassword })) &&( !Validation.rdbNotChecked(new RadioButton[]{rbtnTeacher,rbtnManager})))
            {
                string user = tbUsername.Text;
                string pass = tbPassword.Text;
                if (rbtnTeacher.Checked)
                {
                    Teacher t = Login.TeacherLogin(user, pass);
                    if (t != null)
                    {
                        Query.permission = "Teacher";
                        Query.nameUser = t.Name;
                        Query.userID = t.TeacherID;
                        this.Hide();
                        MainForm main = new MainForm();
                        main.ShowDialog();
                    }
                    else
                    {
                        MessageBox.Show("Username or Password wrong !");
                    }
                }
                if (rbtnManager.Checked)
                {
                    Manager m = Login.ManagerLogin(user, pass);
                    if (m != null)
                    {
                        Query.permission = "Manager";
                        Query.nameUser = m.Name;
                        Query.userID = m.ManagerID;
                        this.Hide();
                        MainForm main = new MainForm();
                        main.ShowDialog();
                        
                    }
                    else
                    {
                        MessageBox.Show("Username or Password wrong !");
                    }
                }                
            }
            else
            {
                MessageBox.Show("Username, Password and Your permission must not be empty", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void tbUsername_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                UserLogin();
            }
            if (e.KeyCode == Keys.Escape)
            {
                Application.Exit();
            }
        }

        private void tbPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                UserLogin();
            }
            if (e.KeyCode == Keys.Escape)
            {
                Application.Exit();
            }
        }
    }
}
